﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.Collections.Generic;
using Jy123.Web.AppCode;

namespace Jy123.Web.Vote
{
    public partial class VoteList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int userID;
            string strUserIdFromCookie = Common.Cookie.getCookie("UserID");
            if (string.IsNullOrEmpty(strUserIdFromCookie))
            {
                Response.Redirect("/");
            }
            userID = int.Parse(strUserIdFromCookie);

            string msg = string.Empty;
            StringBuilder sbHtml = new StringBuilder(Cache["user_votelist"].ToString());//主sb
            #region 页面处理
            //公共部分
            sbHtml = sbHtml.Replace("$ClassSelVoteNew$", " class='sel'")
                .Replace("$ClassSelVoteFriend$", "")
                .Replace("$ClassSelVoteMy$", "")
                .Replace("$ClassSelVoteMyJoin$", "")
                .Replace("$ClassSelVoteHot$", "");

            sbHtml = sbHtml.Replace("$Header$", Cache["user_header"].ToString());//头部
            sbHtml = sbHtml.Replace("$Footer$", Cache["user_footer"].ToString());//底部
            sbHtml = sbHtml.Replace("$Navigation$", Cache["user_navigation_down"].ToString());//左侧导航
            sbHtml = sbHtml.Replace("$LeftUserFriendAdd$", Cache["user_friendadd"].ToString());//左侧快速添加好友
            int voteCounts = 0;
            sbHtml = sbHtml.Replace("$VoteList_li$",VoteList_li(out voteCounts));//显示投票列表
            sbHtml = sbHtml.Replace("$VoteCount$", voteCounts.ToString());

            #endregion
            Response.Write(sbHtml.ToString() + msg);//最终写出整个Html页面
        }
        /// <summary>
        /// 投票列表LI
        /// </summary>
        /// <returns>字符串</returns>
        private string VoteList_li(out int listCounts)
        {
            List<int?> count;
            int currentpage = 1;//当前页
            int pagesize = 4;//页记录大小

            //把被查询页赋值给当前页
            if (!string.IsNullOrEmpty(Request.QueryString["page"]))
            {
                currentpage = Convert.ToInt32(Request.QueryString["page"]);
            }
            StringBuilder sbList = new StringBuilder();
            string strListli = Cache["user_votelist_li"].ToString();
            List<Jy123.Model.V_User_VoteList> listVote = new Jy123.BLL.User.Vote().PaginationVoteList_New(currentpage,pagesize, "*", "AddTime desc",out count);
            foreach (Jy123.Model.V_User_VoteList vote in listVote)
            {
                string str = strListli;
                str = str.Replace("$VoteID$",vote.ID.ToString());
                str = str.Replace("$UserID$",vote.FUser.ToString());
                str = str.Replace("$UserName$", vote.UserName);


                //判断用户是否有头像
                vote.Portrait = new Jy123.BLL.User.SPace().DefaultPortrait(Convert.ToInt32(vote.FUser));

                str = str.Replace("$Portrait$", vote.Portrait);
                str = str.Replace("$JoinPersonCount$",vote.JoinCount.ToString());
                str = str.Replace("$VoteTitle$",vote.Title);
                str = str.Replace("$VoteOption$", VoteListOption(vote.ID));
                sbList.Append(str);
            }
            string pageHtml = @"
                            <li class=""page"">
                                <span>
                                       {0}
                                </span>
                                <b>第{1}-{2}个/共{3}个</b>
                            </li>";
            string strHref = "VoteList.aspx?page=$num$";
            if (count[1] > 1)
            {
                MoonPage mp = new MoonPage(false, true);
                mp.Count = Convert.ToInt32(count[1]);
                sbList.AppendFormat(pageHtml, mp.GetStrPage(currentpage, "no", "sel", strHref), 1 + (currentpage - 1) * pagesize, ((currentpage * pagesize) > count[0]) ? count[0] : currentpage * pagesize, count[0]);
            }
            listCounts = listVote.Count;
            return sbList.ToString();
        }
        /// <summary>
        /// 投票选项列表
        /// </summary>
        /// <param name="VoteID">投票的ID</param>
        /// <returns>字符串</returns>
        private string VoteListOption(int VoteID)
        {
            StringBuilder sbList = new StringBuilder();
            string strHtml = Cache["user_votelist_option"].ToString();
            DataTable dtVoteOption = new Jy123.BLL.User.Vote().VoteOptionList(VoteID, 1, 3, "*", "").Tables[0];
            //List<Jy123.Model.User_VoteOption> listOption = new Jy123.BLL.User_VoteOption().GetModelList("FVote='"+VoteID+"'");
            for (int i = 0; i < dtVoteOption.Rows.Count;i++ )
            {
                string strRow = strHtml;
                strRow = strRow.Replace("$VoteOptionName$", dtVoteOption.Rows[i]["OptionName"].ToString());
                sbList.Append(strRow);
            }
            return sbList.ToString();
        }
    }
}
